package anworld.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

/**
 * 使用@HystrixCommand注解指定当该方法发生异常时调用的方法
 * */
    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable Long id){
            return restTemplate.getForObject("http://eureka-provider/getName",String.class);
    }

    /**
     * hystrix fallback方法
     * @param id id
     * @return 默认的用户
     */
    public String fallback(Long id) {
        return "异常发生，进入fallback方法，接收的参数：id ="+id;
    }

}
